// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/functions/sparksql/might_contain.h>
#include <pollux/common/base/bloom_filter.h>
#include <pollux/functions/sparksql/tests/SparkFunctionBaseTest.h>

namespace kumo::pollux::functions::sparksql::test {
namespace {

class MightContainTest : public SparkFunctionBaseTest {
 protected:
  void testMightContain(
      const std::optional<std::string>& serialized,
      const VectorPtr& value,
      const VectorPtr& expected) {
    // Not using `evaluate()` because the bloom filter binary cannot be parsed
    // by DuckDB parser.
    auto selected = SelectivityVector(value->size());
    std::vector<core::TypedExprPtr> args;
    if (serialized.has_value()) {
      args.push_back(std::make_shared<core::ConstantTypedExpr>(
          VARBINARY(), variant::binary(serialized.value())));
    } else {
      args.push_back(std::make_shared<core::ConstantTypedExpr>(
          VARBINARY(), variant::null(TypeKind::VARBINARY)));
    }
    args.push_back(
        std::make_shared<core::FieldAccessTypedExpr>(BIGINT(), "c0"));
    auto expr = exec::ExprSet(
        {std::make_shared<core::CallTypedExpr>(
            BOOLEAN(), args, "might_contain")},
        &execCtx_);
    auto data = make_row_vector({value});
    exec::EvalCtx evalCtx(&execCtx_, &expr, data.get());
    std::vector<VectorPtr> results(1);
    expr.eval(selected, evalCtx, results);
    pollux::test::assertEqualVectors(expected, results[0]);
  }

  std::string getSerializedBloomFilter(int32_t kSize) {
    BloomFilter bloomFilter;
    bloomFilter.reset(kSize);
    for (auto i = 0; i < kSize; ++i) {
      bloomFilter.insert(melon::hasher<int64_t>()(i));
    }
    std::string data;
    data.resize(bloomFilter.serializedSize());
    bloomFilter.serialize(data.data());
    return data;
  }
};

TEST_F(MightContainTest, basic) {
  constexpr int32_t kSize = 10;
  auto serialized = getSerializedBloomFilter(kSize);
  auto value =
      make_flat_vector<int64_t>(kSize, [](vector_size_t row) { return row; });
  auto expectedContain = make_constant(true, kSize);
  testMightContain(serialized, value, expectedContain);

  auto valueNotContain = make_flat_vector<int64_t>(
      kSize, [](vector_size_t row) { return row + 123451; });
  auto expectedNotContain = make_constant(false, kSize);
  testMightContain(serialized, valueNotContain, expectedNotContain);

  auto values = make_nullable_flat_vector<int64_t>(
      {1, 2, 3, 4, 5, std::nullopt, 123451, 23456, 4, 5});
  auto expected = make_nullable_flat_vector<bool>(
      {true, true, true, true, true, std::nullopt, false, false, true, true});
  testMightContain(serialized, values, expected);
}

TEST_F(MightContainTest, nullBloomFilter) {
  auto value = make_flat_vector<int64_t>({2, 4});
  auto expected = make_null_constant(TypeKind::BOOLEAN, value->size());
  testMightContain(std::nullopt, value, expected);
}
} // namespace
} // namespace kumo::pollux::functions::sparksql::test
